<?php
/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */
class Default_Model_NephiteExchangeData
{
	protected $_mapper = null;
	protected $_exchangeRates = null;
	protected $_lastUpdated = null;
	protected $_barleyPrice = null;
	protected $_currencies = null;
	
    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid NephiteExchangeData property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid NephiteExchangeData property');
        }
        return $this->$method();
    }
    
    public function getMapper()
    {
        if (null === $this->_mapper) {
            $this->setMapper(new Default_Model_NephiteExchangeDataMapper());
        }
        return $this->_mapper;
    }
    
    public function setMapper (Default_Model_NephiteExchangeDataMapper $mapper) {
    	$this->_mapper = $mapper;
		return $this;
    }
	
	public function getLastUpdated () {
		if ($_lastUpdated === null) {
			$this->getMapper()->setLastUpdated($this);
		}
		
		return $this->_lastUpdated;
	}
	
	public function setLastUpdated ($date) {
		$this->_lastUpdated = $date;
		return $this;
	}
	
	public function getLatestBarleyPrice () {
		if ($_barleyPrice === null) {
			$this->getMapper()->setBarleyPrice($this);
		}
		
		return $this->_barleyPrice;	
	}
	
	public function setLatestBarleyPrice ($price) {
		$this->_barleyPrice = $price;
		return $this;
	}
	
	public function getExchangeRates ($searchString) {
		if ($_exchangeRates === null) {
			$this->getMapper()->fetchExchanges($this, $searchString);
		}
		
		return $this->_exchangeRates;		
	}
	
	public function setExchangeRates ($rates) {
		$this->_exchangeRates = $rates;
		return $this;
	}
	
	public function convertPrice ($ISOName, $amount) {
		$rates = $this->getExchangeRates($ISOName);
		
		if (!$rates[0]) {
			return -1;
		}
		
		if ($this->getLatestBarleyPrice() <= 0) {
			return -1;
		}

		return $rates[0]->convertPriceToArray ($amount, $this->getLatestBarleyPrice());
	}
	
	public function getCurrencies () {
		if ($this->_currencies == null) {
			$this->getMapper()->getCurrencies ($this);
		}
		
		return  $this->_currencies;
	}
	
	public function setCurrencies ($currencies) {
		$this->_currencies = $currencies;
	}
	
	public function addComment ($email, $comment) {
		$this->getMapper()->addComment ($email, $comment);
	}
	
}